﻿using System;
using System.Collections.Generic;
using Kugar.Tool.RemoteEventBus.Subscibers;
using Kugar.Tool.RemoteEventBus.Triggers;
using RabbitMQ.Client;

namespace Kugar.Tool.RemoteEventBus
{
    /// <summary>
    /// 远程事件管理
    /// </summary>
    public interface IRemoteEventBus
    {
        /// <summary>
        /// 当前RabbitMQ连接
        /// </summary>
        IConnection Connection { get; }

        /// <summary>
        /// 当前节点ID
        /// </summary>
        string ServiceID { get; }

        IEnumerable<IEventTrigger> Triggers { get; }

        IEnumerable<IEventSubsciber> Subscribers { get; }

        /// <summary>
        /// 远程节点离线事件
        /// </summary>
        event EventHandler<NodeOfflineEventArgs> NodeOffline;

        /// <summary>
        /// 远程节点上线通知
        /// </summary>
        event EventHandler<NodeOnlineEventArgs> NodeOnline;

        
        /// <summary>
        /// 同步订阅器接收到一个事件
        /// </summary>
        event EventHandler<SyncEventRaisedEventArgs> SyncEventRaised;

        /// <summary>
        /// 异步订阅器接收到一个事件
        /// </summary>
        event EventHandler<AsyncEventRaisedEventArgs> AsyncEventRaised;

        /// <summary>
        /// 同步触发器接收到一个客户的回复信息
        /// </summary>
        event EventHandler<ClientEventHandeEventArgs> SyncEventClientHandled;

        /// <summary>
        /// 异步触发器接收到一个客户端的回复信息
        /// </summary>
        event EventHandler<ClientEventHandeEventArgs> AsyncEventClientHandled;

        /// <summary>
        /// 同步触发器触发一个事件
        /// </summary>
        event EventHandler<EventTriggerEventArgs> SyncEventTrigger;

        /// <summary>
        /// 异步触发器触发一个事件
        /// </summary>
        event EventHandler<EventTriggerEventArgs> AsyncEventTrigger;

        /// <summary>
        /// 退出异步事件
        /// </summary>
        void Close();

        /// <summary>
        /// 获取一异步事件订阅器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventName"></param>
        /// <returns></returns>
        AsyncEventSubsciber<T> GetAsyncEventSubsciber<T>(string eventName); //where T : EventArgs;

        /// <summary>
        /// 获取一个异步事件触发器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventName"></param>
        /// <returns></returns>
        AsyncEventTrigger<T> GetAsyncEventTrigger<T>(string eventName); // where T : EventArgs;

        /// <summary>
        /// 获取一个同步事件订阅器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventName"></param>
        /// <returns></returns>
        SyncEventSubsecriber<T> GetSyncEventSubsecriber<T>(string eventName) where T : SyncRaiseEventArgs;

        /// <summary>
        /// 获取一个同步事件触发器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventName"></param>
        /// <returns></returns>
        SyncEventTrigger<T> GetSyncEventTrigger<T>(string eventName) where T : EventArgs;

        ///// <summary>
        ///// 在程序初始化完成后,调用该函数,调用后,将会完成整个类的初始化过程,并将向外发送节点上线通知,建议在调用该函数前,将所需的事件订阅器都创建完,避免每次创建时都去通知其他节点
        ///// </summary>
        //void InitCompleted();

        /// <summary>
        /// 初始化当前实例,传入RabbitMQ的链接,当前服务的名称ID
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="serviceID"></param>
        void Initialize(IConnection conn, string serviceID);
    }
}